<?php

class Db_Pdo {
	
	public $conn;
	private $config;
	var $querynum = 0;
	const DEFAULT_CAHRSET = 'utf8';
	
	function __construct($config) {
		
		if ($config) {
			$this->config = $config;
		} else {
			exit ( 'PDO ERROR: Cound not found config!' );
		}
		try {
			$this->conn = new PDO ( $config ['engine'] . ':host=' . $config ['host'] . ';dbname=' . $config ['database'], $config ['username'], $config ['password'], array (PDO::ATTR_PERSISTENT => true ) );
			$charset = isset ( $config ['charset'] ) ? $config ['charset'] : self::DEFAULT_CAHRSET;
			$this->conn->exec ( "SET NAMES '{$charset}';" );
		} catch ( PDOException $error ) {
			$this->conn = false;
			$this->errorMessage = $error->getMessage ();
			$this->halt ( 'PDO Conn Error:', $this->errorMessage );
		}
		
		if ($this->conn) {
			$this->conn->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
		}
		
		if ($this->conn && $config ['database']) {
			$this->database = $config ['database'];
		}
		if (DEBUG === TRUE) {
			echo '--- PDO DB construct!' . '<br/>';
		}
	}
	
	function is_connected() {
		return ($this->conn !== false);
	}
	
	function close() {
		return $this->disconnect ();
	}
	
	function disconnect() {
		if ($this->conn) {
			$this->conn = null;
		}
	}
	
	function select_db($db) {
		if ($this->conn) {
			$this->database = $db;
			return true;
		}
	}
	
	function query($sql) {
		if ($this->conn) {
			$queryResult = $this->conn->prepare ( $sql );
			
			if ($queryResult)
				$queryResult->execute ();
			
			if (! $queryResult) {
				$errorInfo = $this->conn->errorInfo ();
				$this->errorMessage = $errorInfo [2];
				$this->halt ( 'PDO Error:', $this->errorMessage );
			}
			$this->querynum ++;
			return $queryResult;
		} else {
			return false;
		}
	}
	
	function num_rows($query) {
		if (! $query)
			return false;
		if ($this->conn) {
			return count ( $query->fetchAll () );
		}
	}
	
	function query_first($sql) {
		$query = $this->fetch_array ( $this->query ( $sql ) );
		return $query;
	}
	
	function result_first($sql) {
		return $this->result ( $this->query ( $sql ), 0 );
	}
	
	function fetch_row($query) {
		if (! $query)
			return false;
		
		if ($this->conn) {
			return $query->fetch ( PDO::FETCH_NUM );
		}
	}
	
	function fetch_array($query) {
		if (! $query)
			return false;
		if ($this->conn) {
			return $query->fetch ( PDO::FETCH_ASSOC );
		}
	}
	
	function fetch_all($query) {
		if (! $query)
			return false;
		if ($this->conn) {
			return $query->fetchAll ( PDO::FETCH_ASSOC );
		}
	}
	
	function fetch_fields($query) {
		//if (! $query)
		return false;
		/*if ($this->conn) {
			return $query->fetch ( PDO::FETCH_INTO );
		}*/
	}
	
	function affected_rows($query) {
		if (! $query)
			return false;
		if ($this->conn) {
			return $query->rowCount ();
		}
	}
	
	function num_fields($query) {
		return $query->columnCount ();
	}
	
	function result($query, $targetRow, $targetColumn = "") {
		if (! $query)
			return false;
		
		if ($this->conn) {
			if ($targetColumn) {
				$resultRow = $query->fetch ( PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $targetRow );
				return $resultRow [$targetColumn];
			} else {
				$resultRow = $query->fetch ( PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, $targetRow );
				return $resultRow [0];
			}
		}
	}
	
	function free_result() {
		return true;
	}
	
	function list_db() {
		if ($this->conn) {
			return $this->query ( "SHOW DATABASES" )->fetchAll ( PDO::FETCH_ASSOC );
		}
	}
	
	function list_table() {
		if ($this->conn) {
			return $this->query ( "SHOW TABLES" )->fetchAll ( PDO::FETCH_ASSOC );
		}
	}
	
	function is_charset_supported() {
		if ($this->conn) {
			if (version_compare ( $this->getVersion (), "4.1", ">" )) {
				return true;
			} else {
				return false;
			}
		}
	}
	
	function list_charset() {
		if ($this->conn) {
			return $this->query ( "SHOW CHARACTER SET" )->fetchAll ();
		}
	}
	
	function list_collation() {
		if ($this->conn) {
			return $this->query ( "SHOW COLLATION" )->fetchAll ();
		}
	}
	
	function insert_id() {
		if ($this->conn) {
			return $this->conn->lastInsertId ();
		}
	}
	
	function escape_string($toEscape) {
		if ($this->conn) {
			$toEscape = $this->conn->quote ( $toEscape );
			$toEscape = substr ( $toEscape, 1, - 1 );
			return $toEscape;
		}
	}
	
	function version() {
		return false;
	}
	
	function error() {
		return $this->errorMessage;
	}
	
	function errno() {
		return $this->conn->errorCode ();
	}
	
	function halt($message = '', $sql = '') {
		echo $message . "<br / >";
		echo $sql;
		exit ();
	}

}
?>